/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.system.impl;

import dao.system.SystemNumberingDao;
import java.util.Date;
import model.system.SystemNumbering;
import model.system.SystemNumberingGen;
import model.system.SystemNumberingGenId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.system.SystemNumberingService;
import utility.StringUtils;
import utility.typeSafe.NumberingFormat;

/**
 *
 * @author jay
 */
@Service("systemNumberingService")
@Transactional(readOnly=true)
public class SystemNumberingServiceImpl implements SystemNumberingService{

    @Autowired private SystemNumberingDao numberingDao;
    
    @Transactional(readOnly=false)
    @Override
    public String getNextNumber(int id, java.util.Date date) {
        SystemNumbering numbering = numberingDao.getById(id);
        String replaceWithNextNumber="REPLACE_WITH_NUMBER";
        String tmp = null;
        String theFormat=numbering.getFormat();
        
        int number_size = 0;
        int can_set_number_pos=1;
        int number_pos=0;
        String[] formats = theFormat.split("/");
        for (String partOfFormat : formats) {
            if(partOfFormat.equals(NumberingFormat.COMPANY_NAME.toString())) { tmp = "BTP"; }
            else if(partOfFormat.equals(NumberingFormat.TRANS_NAME.toString())) { tmp = NumberingFormat.TRANS_NAME.toString(); }
            else if(partOfFormat.equals(NumberingFormat.YEAR_PERIOD.toString())) { tmp = StringUtils.formatDateCustom(date, "yy"); }
            else if(partOfFormat.equals(NumberingFormat.YEARFULL_PERIOD.toString())) { tmp = StringUtils.formatDateCustom(date, "yyyy");  }
            else if(partOfFormat.equals(NumberingFormat.MONTH_PERIOD.toString())) { tmp = StringUtils.formatDateCustom(date, "MM"); } 
            else if(partOfFormat.equals(NumberingFormat.PC_ID.toString())) { tmp = partOfFormat; }
            else {
                if(partOfFormat.substring(0, 2).equals(NumberingFormat.NUMBER_SEQ.toString())) {
                    number_size = Integer.parseInt(partOfFormat.substring(2, 3));
                    can_set_number_pos = 0;
                    tmp = replaceWithNextNumber;
                }
            }
            theFormat = theFormat.replaceAll(partOfFormat, tmp);
            
            int tmpLength = tmp.length();
            int lmtLength = numbering.getFormatDelimiter()==null ? 0 : numbering.getFormatDelimiter().length();
            if (can_set_number_pos==1) { number_pos=number_pos+tmpLength + lmtLength; }
        }
       
        theFormat = theFormat.replaceAll("/", numbering.getFormatDelimiter()==null ? "" : numbering.getFormatDelimiter());
            
        //mulai penomoran
        String generatorValue = null, 
                formatGenerator = numbering.getFormatGenerator()==null ? null : numbering.getFormatGenerator();
        
        if(formatGenerator==null) {generatorValue = "STD";}
        else {
            
            String[] formatGenerators = numbering.getFormatDelimiter().split("/");
            for (String partOfFormat : formatGenerators) {
                
                generatorValue = formatGenerator;
                if(partOfFormat.equals(NumberingFormat.YEAR_PERIOD.toString())) { 
                    generatorValue = formatGenerator.replaceAll(partOfFormat, 
                    StringUtils.formatDateCustom(date, "yy")); 
                
                }else if(partOfFormat.equals(NumberingFormat.YEARFULL_PERIOD.toString())) { 
                    generatorValue = formatGenerator.replaceAll(partOfFormat, 
                    StringUtils.formatDateCustom(date, "yyyy")); 
                
                }else if(partOfFormat.equals(NumberingFormat.MONTH_PERIOD.toString())) { 
                    generatorValue = formatGenerator.replaceAll(partOfFormat, 
                    StringUtils.formatDateCustom(date, "MM"));
                }else if(partOfFormat.equals(NumberingFormat.PC_ID.toString())) { 
                    generatorValue = formatGenerator.replaceAll(partOfFormat, 
                    StringUtils.formatDateCustom(date, "MM"));
                }  
                formatGenerator = generatorValue;
            }
        }
        
        SystemNumberingGen newNumberGen = new SystemNumberingGen(
                new SystemNumberingGenId(numbering.getId(), generatorValue), numbering, 
                theFormat.replaceAll(replaceWithNextNumber, "000000000".substring(0, number_size)));
        
        for(SystemNumberingGen gn : numbering.getSystemNumberingGens()) {
            if (generatorValue.equals(gn.getId().getGeneratorValue())) {
                newNumberGen = gn;
            }
        }
        
        int newSeq;
        newSeq = Integer.parseInt(newNumberGen.getCurrentNo().substring(number_pos, number_pos+number_size)) +1;
      
        String newNumberFormat = "000000000"+newSeq;
        newNumberFormat = newNumberFormat.substring(newNumberFormat.length() - number_size, newNumberFormat.length());
        newNumberFormat = theFormat.replaceAll(replaceWithNextNumber, newNumberFormat);
        
        newNumberGen.setCurrentNo(newNumberFormat);
        
        if (numbering.getSystemNumberingGens().isEmpty()) {
            numbering.getSystemNumberingGens().add(newNumberGen);
        }
        
        if (numberingDao.save(numbering)) { return newNumberFormat; }
        return "";
    }
    

    @Transactional(readOnly=false)
    @Override
    public String getNextNumberDb(int id, Date date) {
        return numberingDao.getNextNumberDb(id, date);
    }
    
}
